//
//  DeviceManager.m
//  TooToo
//
//  Created by MoHao on 15/6/19.
//  Copyright (c) 2015年 MoHao. All rights reserved.
//

#import "DeviceManager.h"

#import "KeychainHelper.h"

static NSString * ServiceName = @"TooTooDevice";

@implementation DeviceManager

static DeviceManager *sharedAccountManagerInstance = nil;

+ (DeviceManager *)sharedInstance {
    
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        
        sharedAccountManagerInstance = [[self alloc] init];
    });
    
    return sharedAccountManagerInstance;
}

-(NSString *)getDeviceId
{
    NSMutableDictionary *keyDict = (NSMutableDictionary *)[KeychainHelper getKeychainByName:ServiceName];
    NSString *key = SafeGetStringValue([keyDict objectForKey:ServiceName]);
    
    if (!key || key.length == 0) {
        
        NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
        [usernamepasswordKVPairs setObject:identifierStr forKey:ServiceName];
        //存
        [KeychainHelper saveKeychainByKeyName:ServiceName value:usernamepasswordKVPairs];
        
        NSLog(@"第一次存Key = %@",identifierStr);
        
        return identifierStr;
    }
    
//    NSLog(@"已存Key = %@",key);
    
    return key;
}

@end
